

package com.zdkj.subway.stm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.entity.SwStCycle;
import com.zdkj.subway.stm.enums.CycleEnum;
import com.zdkj.subway.stm.fegin.SupportService;
import com.zdkj.subway.stm.service.SwStCycleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 调票通知表
 *
 * @author chenbo
 * @date 2020-02-06 19:51:24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstcyclenotice")
@Slf4j
@Api(value = "swstcyclenotice", tags = "调票通知表管理")
public class SwStCycleNoticeController {

    private static final String NOTICE_STATUS_SUC = "1";

    private final SwStCycleService swStCycleService;
    private final SupportService supportService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param swStCycle 调票通知表
     * @return
     */
    @ApiOperation(value = "分页关联查询", notes = "分页关联查询")
    @GetMapping("/pageMapper")
    public R getSwStCycleNoticePage(Page page, SwStCycle swStCycle) {

        return R.ok();
    }

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param swStCycle 调票通知表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getPage(Page page, SwStCycle swStCycle) {
        String lineCode = SecurityUtils.getUser().getLineCode();
        String stationCode = SecurityUtils.getUser().getStationCode();
        IPage pageReturn = swStCycleService.page(page,
                Wrappers.query(swStCycle).orderByDesc("out_time").lambda()
                        .eq(SwStCycle::getNoticeStatus, NOTICE_STATUS_SUC)
                        .and(i -> i.eq(SwStCycle::getOutLineCode,lineCode)
                                .eq(SwStCycle::getOutStationCode, stationCode)
                                .or().eq(SwStCycle::getInLineCode,lineCode).eq(SwStCycle::getInStationCode, stationCode)));
        List<SwStCycle> records = pageReturn.getRecords();
        for (SwStCycle record : records) {
            /**
             * 如果是调入站   因为查询出来  code 已经转为了name 此处使用 name 比较
             */
            if (record.getInLineCode().equals(lineCode) && record.getInStationCode().equals(stationCode)) {
                /**
                 * 如果调出未确认或者调入已经确认过了前端不显示确认按钮
                 */
                if (record.getIsConfirmOut().equals(CycleEnum.CONFIRM_FAIL.getCode())) {
                    record.setIsVisible(CycleEnum.VISIBLE_SUC.getCode());
                } else if (record.getIsConfirmIn().equals(CycleEnum.CONFIRM_SUC.getCode())) {
                    record.setIsVisible(CycleEnum.VISIBLE_SUC.getCode());
                } else {
                    record.setIsVisible(CycleEnum.VISIBLE_FAIL.getCode());
                }
                /**
                 * 如果是调出站
                 */
            } else if (record.getOutLineCode().equals(lineCode) && record.getOutStationCode().equals(stationCode)) {
                if (record.getIsConfirmOut().equals(CycleEnum.CONFIRM_SUC.getCode())) {
                    record.setIsVisible(CycleEnum.VISIBLE_SUC.getCode());
                } else {
                    record.setIsVisible(CycleEnum.VISIBLE_FAIL.getCode());
                }
            } else {
                log.info("调票通知else，基本不会执行到此");
            }
        }
        return R.ok(pageReturn);
    }

    /**
     * 确认通知
     * 一条记录失败就返回
     * @param swStCycle 确认通知
     * @return R
     */
    @ApiOperation(value = "调票确认", notes = "调票确认")
    @SysLog("调票确认")
    @PutMapping("/confirm")
    @PreAuthorize("@pms.hasPermission('stm_sw_st_cycle_notice_confirm')")
    public R confirm(@RequestBody List<SwStCycle> swStCycle) {
        //获取线路和站点
        String lineCode = SecurityUtils.getUser().getLineCode();
        String stationCode = SecurityUtils.getUser().getStationCode();
        for (SwStCycle stCycle : swStCycle) {
            SwStCycle byId = swStCycleService.getById(stCycle.getId());
            //必须判断线路和站点,否则站点编号冲突
            if (byId.getInLineCode().equals(lineCode) && byId.getInStationCode().equals(stationCode)) {
                if (byId.getIsConfirmOut().equals(CycleEnum.CONFIRM_FAIL.getCode())){
                    return R.failed("请等待调出站先确认");
                }
                byId.setIsConfirmIn(CycleEnum.CONFIRM_SUC.getCode());
                swStCycleService.updateById(byId);
            } else if (byId.getOutLineCode().equals(lineCode) && byId.getOutStationCode().equals(stationCode)) {
                byId.setIsConfirmOut(CycleEnum.CONFIRM_SUC.getCode());
                swStCycleService.updateById(byId);
            } else {
                return R.failed("调票确认失败");
            }
        }

        return R.ok();
    }
}
